<?php

namespace App\Service\Customer;
use App\Models\Follow\FollowModel;
use DB;

class CustomerRelateService
{
    public static function addFollow(array $data)
    {
        if(!FollowModel::insert($data))
        {
            return false;
        }
        return true;
    }

    public static function findFollow($uid, $fuid)
    {
		
        return FollowModel::where(['uid'=>$uid,'fuid'=>$fuid])->first();
    }
	
	/**
     * 取消关注
     */
    public static function removeFollow($uid, $fuid)
    {

        if(!FollowModel::where(['uid'=>$uid,'fuid'=>$fuid])->delete())
        {
            return false;
        }
        return true;
    }
	
	public static function getFollowAuthorArticle($uid, $page, $offset)
    {
        $result = DB::select("select
(select count(*) from lsj_praise_log b where b.articleid=a.id)  as praise_num,
(select count(*) from lsj_comment_log b where a.id=b.worksid) as comment_num,
(select count(*) from lsj_order b where a.id=b.article_id and b.paystatus=1) as order_num,
(select count(*) from lsj_praise_log b where b.articleid=a.id and b.uid= ?) as is_praise,
(select count(*) from lsj_order b where a.id=b.article_id and b.paystatus=1 and b.uid= ?) as is_buy,
a.id,a.title,a.introduction,a.price,a.thumb,a.create_time,a.author_uid,a.reading,a.article_class,b.nickname as author,b.headimgurl,1 as is_follow,buy_class
from 
(select * from lsj_article where author_uid in(
select fuid from lsj_follow where uid=? ) and status=1 order by id desc limit ?,?)a
left join lsj_user b on a.author_uid=b.uid 
order by a.id desc", [$uid,$uid,$uid,$offset,$page]);
        return $result;
    }
	
	//获取粉丝
    public static function getUserFans($uid,$page,$offset)
    {
        $fans = DB::select("select 
(select count(*) from lsj_follow where uid=?) as is_follow,
b.nickname,a.fuid as author_uid,a.uid as fans_uid,b.headimgurl
from lsj_follow a,lsj_user b where fuid=? and a.uid=b.uid
limit ?,?", [$uid,$uid,$offset,$page]);
		
        return $fans;
    }
	//我的关注
    public static function getUserFollows($uid,$page,$offset)
    {
        $follows = DB::select("select 
b.nickname,a.uid as user_uid,a.fuid as follow_uid,b.headimgurl
from lsj_follow a,lsj_user b where a.uid=? and a.fuid=b.uid
limit ?,?", [$uid,$offset,$page]);
        return $follows;
    }
	
	//系统推荐人
    public static function getRecommendAuthors($uid)
    {
        /*$recommends = DB::select("select uid,nickname,headimgurl,
(select count(*) from lsj_follow b where lsj_user.uid=b.fuid) as fans
from lsj_user inner join (SELECT ROUND(RAND() * (SELECT MAX(id) FROM `lsj_user`)) AS id) as rand_table on lsj_user.id>=rand_table.id
where is_author=1 and status=1 
and uid not in(select fuid from lsj_follow where uid=?) 
and uid!=?
limit 6",[$uid,$uid]);*/
		$recommends = DB::select("select 
(select count(*) from lsj_follow b where b.fuid=a.uid) as fans_num,uid,nickname,headimgurl,introduce,1 is_author,
(select count(*) from lsj_follow b where b.fuid=a.uid and b.uid=?)  as is_follow
from lsj_user a 
where uid in ('sj_14994145561001','sj_14994146054001','sj_14994146414001','sj_14994146821001','sj_14994147104001','sj_14994204623001')",[$uid]);
		return $recommends;
    }
}